//
//  UIImage+Addition.m
//  YunHaiManage
//
//  Created by zhangbolin on 2021/12/17.
//

#import "UIImage+Addition.h"
#import "UIBezierPath+Bezier.h"

@implementation UIImage (Addition)

+ (UIImage *)gradientImageWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor size:(CGSize)size {
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,[UIScreen mainScreen].scale);
    
    NSArray *colors = @[(__bridge id)(startColor.CGColor),(__bridge id)(endColor.CGColor)];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat location[] = {0.0f,1.0f};
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, location);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextDrawLinearGradient(context, gradient, CGPointMake(size.width / 2.0, 0), CGPointMake(size.width / 2.0, size.height), 0);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
    
    return image;
}

- (UIImage *)stretchAroundWithContainerSize:(CGSize)size edgeInsets:(UIEdgeInsets)edgeInsets {
    CGSize imageSize = self.size;
    CGSize bgSize = size;
    
    //1.第一次拉伸
    UIImage *image = [self stretchableImageWithLeftCapWidth:edgeInsets.right topCapHeight:edgeInsets.top];
    
    //第一次拉伸的距离之后图片总宽度
    CGFloat tempWidth = (bgSize.width)/2 + imageSize.width/2;
    CGFloat tempHeight = (bgSize.height)/2 + imageSize.height/2;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(tempWidth, tempHeight), NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0, tempWidth, tempHeight)];
    
    //拿到拉伸过的图片
    UIImage *firstStrechImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //2.第二次拉伸
    UIImage *secondStrechImage = [firstStrechImage stretchableImageWithLeftCapWidth:firstStrechImage.size.width - edgeInsets.left topCapHeight:firstStrechImage.size.height - edgeInsets.bottom];
    
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [secondStrechImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //拿到拉伸过的图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return finalImage;
}

+ (UIImage *)imageByApplyingAlpha:(CGFloat)alpha image:(UIImage*)image {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    CGContextSetAlpha(ctx, alpha);
    CGContextDrawImage(ctx, area, image.CGImage);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (UIImage *)gradiantImageWithSize:(CGSize)imageSize
                     corrnerRadius:(CGFloat)raduius
                    gradientColors:(NSArray *)colors
                        percentage:(NSArray *)percents
                      gradientType:(id)gradientType
                          isBorder:(BOOL)isBorder {
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    CGContextSetShouldAntialias(context, YES);
    
    UIBezierPath *pathOut = nil;
    if (isBorder) {
        pathOut = [UIBezierPath qjn_bezierPathWithRoundedRect:rect cornerRadius:raduius];
        [[UIColor whiteColor] setFill];
        UIBezierPath *pathInside = [UIBezierPath qjn_bezierPathWithRoundedRect:CGRectInset(rect, 1, 1) cornerRadius:raduius - 1];
        [pathOut appendPath:[pathInside bezierPathByReversingPath]];
    }else {
        pathOut = [UIBezierPath qjn_bezierPathWithRoundedRect:rect cornerRadius:raduius];
        [[UIColor whiteColor] setFill];
    }
    [pathOut fill];
    [pathOut addClip];
    
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.locations = percents;
    
    NSMutableArray *cgColors = [NSMutableArray array];
    for(UIColor *color in colors) {
        [cgColors addObject:(__bridge id)color.CGColor];
    }
    
    layer.colors = cgColors;
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(1, 0);
    layer.frame = rect;
    [layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
